package com.llkj.qingnianguoxue.mine;

import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.BankSpinnerAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.LoginActivity;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithRawalActivity extends BaseActivity {

    @BindView(R.id.flBankChoice)
    RelativeLayout flBankChoice;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.etBankNo)
    EditText etBankNo;//银行卡号
    @BindView(R.id.etEnableMoney)
    EditText etEnableMoney;//提现金额
    @BindView(R.id.etPhone)
    EditText etPhone;//手机号
    @BindView(R.id.etCode)
    EditText etCode;//验证码
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;//获取验证码
    private int mCurrentPosition;
    BankSpinnerAdapter spinnerAdapter;
    private List<String> mBankNameData;
    private TimeCount time;//倒计时
    private int durationTime = 60 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_rawal);
        setTitle("提现", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        ButterKnife.bind(this);

        time = new TimeCount(durationTime, 1000);
        initView();
    }

    private void initView(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        String withdrawls = MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_WITHDRAWALS);
        String phone = MyApplication.getInstance().sputil.getString(ConstantUserInfo.MOBILE);
        etPhone.setText(phone);
        etPhone.setEnabled(false);
        etEnableMoney.setHint(String.format(getString(R.string.withrawal_money),withdrawls));
        mBankNameData = new ArrayList<String>();
        addBanks();
        //适配器
        spinnerAdapter = new BankSpinnerAdapter(this,mBankNameData);
        //设置样式
        //加载适配器
        spinner.setAdapter(spinnerAdapter);
        spinner.setSelection(mCurrentPosition, true);
        spinner.setDropDownVerticalOffset(DisplayUtil.dip2px(this,45));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mCurrentPosition = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    @OnClick({R.id.tvGetCode, R.id.flBankChoice, R.id.tvApplyWithRawal})
    public void onClick(View view){
        String phone = null;
        switch (view.getId()){
            case R.id.tvGetCode://获取验证码
                phone = etPhone.getText().toString();
                if(!StringUtils.isPhoneNumber(phone)){
                    ToastUtil.makeLongText(this,"请输入正确的手机号");
                    return;
                }
                time.start();
                loadWaitDialog();
                HttpMethod.loginFormcode(this, this, phone, "2");
                break;
            case R.id.tvApplyWithRawal://申请提现
                if(mCurrentPosition<1){
                    ToastUtil.makeLongText(this,getString(R.string.choice_bank));
                    return;
                }
                String bankNo = etBankNo.getText().toString();
                if(StringUtils.isEmpty(bankNo)){
                    ToastUtil.makeLongText(this,getString(R.string.input_bank_no));
                    return;
                }
                String money = etEnableMoney.getText().toString();
                if(StringUtils.isEmpty(money)){
                    ToastUtil.makeLongText(this,"请输入提现金额");
                    return;
                }
                try {
                    if(Integer.parseInt(money)%50!=0){
                        ToastUtil.makeLongText(this,"提现金额为50的倍数");
                        return;
                    }
                }catch (Exception e){}
                phone = etPhone.getText().toString();
                if(!StringUtils.isPhoneNumber(phone)){
                    ToastUtil.makeLongText(this,"请输入正确的手机号");
                    return;
                }
                String code = etCode.getText().toString();
                if(StringUtils.isPhoneNumber(code)){
                    ToastUtil.makeLongText(this,getString(R.string.register_input_code));
                    return;
                }
                loadWaitDialog();
                HttpMethod.withdrawals(this,this,mBankNameData.get(mCurrentPosition),bankNo,money,phone,code);
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_USER_WITHDRAWALS) {//用户提现
            ToastUtil.makeLongText(this,getString(R.string.withrawal_request));
            finish();
        }else  if (method == HttpMethod.HTTP_LOGIN_FORMCODE) {//
            ToastUtil.makeLongText(this,"验证码已发送，请注意查收");
        }
    }

    private void addBanks(){
        mBankNameData.add("请选择开户行");
        mBankNameData.add("中国工商银行");
        mBankNameData.add("中国建设银行");
        mBankNameData.add("北京银行");
        mBankNameData.add("中国农业银行");
        mBankNameData.add("中国交通银行");
        mBankNameData.add("中国银行");
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            if (tvGetCode != null) {
                tvGetCode.setText("重新发送");
                tvGetCode.setClickable(true);
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            if (tvGetCode != null) {
                tvGetCode.setClickable(false);//防止重复点击
                tvGetCode.setText(millisUntilFinished / 1000 + "s");
            }
        }
    }
}
